//预留个人信息
import { getYaliUserInfo } from '../../../../utils/checkAuth';
import getIn from '../../../../yali-sdk/utils/getIn';
import immutable from '../../../../yali-sdk/lib/immutable';
import frequentClick from '../../../../yali-sdk/utils/frequentClick';
import apiSaveAnchor from '../../../../yali-sdk/business/live/api/apiSaveAnchor';
import { FAIL } from '../../../../yali-sdk/utils/fail';
import { SUCCESS } from '../../../../yali-sdk/utils/success';
import { filterImg } from '../../../imgBaseUrl';
Page({
    onLoad:function(options) {
        let fresh =this.options.fresh?this.options.fresh:'';
        const self =this;
        self.geUserData(fresh,self);
    },
   
    radioChange(e) {
        const index = getIn(e, ['target', 'dataset', 'index']);
        let { info } = this.data;
        info[index].value = Number(e.detail.value);
        this.setData({
            info,
        });
        
    },
    bindinput(e) {
        const index = getIn(e, ['target', 'dataset', 'index']);
        let { info } = this.data;
        info[index].value = e.detail.value;
        this.setData({
            info,
        });
    },
    bindTextAreaBlur(e){
        const index = getIn(e, ['target', 'dataset', 'index']);
        let { info } = this.data;
        info[index].value = e.detail.value;
        this.setData({
            info,
        });  
    },
    onOpenEdit() {
        frequentClick().then(() => {
          const initInfo = immutable.fromJS(this.data.info).toJS();
            this.setData({
                isEdit: true,
                initInfo,
            });
        });
    },
    onOkEdit(e) {
         wx.showLoading();
        frequentClick().then(() => {
            const self = this ;
            const id =this.options.anchor_id;
            const params = [...this.data.info ];
            const new_params ={id};
            params && params.forEach(function(value,index,array) {
            new_params[value.type] =value.value ;
           })
        
            global.actionDispatcher.execute(apiSaveAnchor,new_params, (status, result) => {
               if(status == SUCCESS) {
                 wx.hideLoading();
                 wx.showToast({title:result.msg, icon: 'none', duration: 1500 });

                self.setData({  
                        info: this.data.initInfo,
                        isEdit: false,
                    }); 
                wx.reLaunch({
                    url: '/pages/anchor/center/preson/preson?fresh=true&anchor_id='+id
                 })  
             
               }else if(status == FAIL){
                 wx.hideLoading();
                 wx.showToast({title:result.msg, icon: 'none', duration: 1500 });
               }     
            });
        });
    },
    onCancelEdit() {
        frequentClick().then(() => {
            this.setData({
                info: this.data.initInfo,
                isEdit: false,
            });
        });
    },
    geUserData(fresh,self){
        getYaliUserInfo({forceRefresh: fresh}).then((user) => {
                let new_user = filterImg(user, 'head_photo');
                const phone = wx.getSystemInfoSync();
                    self.setData({
                        user:new_user,
                        phone,
                        info: [
                          {
                            type: 'name',
                            name: '名称',
                            value: user.name,
                            keyType: 'text',
                            maxlength: 25
                          },
                          {
                            type: 'sex',
                            name: '性别',
                            value: user.sex,
                            isSex: true,
                            keyType: 'number',
                            maxlength: 3
                          },
                          {
                            type: 'height',
                            name: '身高',
                            unit: 'cm',
                            value: user.height,
                            keyType: 'digit',
                            maxlength: 3
                          },
                          {
                            type: 'weight',
                            name: '体重',
                            unit: 'kg',
                            value: user.weight,
                            keyType: 'digit',
                            maxlength: 3
                          },
                          {
                            type: 'bust',
                            name: '胸围',
                            unit: 'cm',
                            value: user.bust,
                            keyType: 'digit',
                            maxlength: 3
                          },
                          {
                            type: 'waist',
                            name: '腰围',
                            unit: 'cm',
                            value: user.waist,
                            keyType: 'digit',
                            maxlength: 3
                          },
                          {
                            type: 'hip',
                            name: '臀围',
                            unit: 'cm',
                            value: user.hip,
                            keyType: 'digit',
                            maxlength: 3
                          },
                          {
                            type: 'signature',
                            name: '签名',
                            isTextarea: true,
                            value: user.signature,
                            maxlength: 80
                          },
                        ],
             });
       });
  }

});


  